// pages/index/findschool/findschool.js

import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    GetSchoolList, // 查院校
} from '../../../api/index'

Page({

    
    /**
     * 页面的初始数据
     */
    data: {
        listArray: [],
        itemArray: ['综合类', '强基', '985', '211', '双一流'],
        screenArray: ['本+专', '省份', '类型', '其他'],
        screenType: 0, // 0 学历  1省份  2类型  3其他
        show: false,
        lotArray: ['本+专', '本科', '专科'],
        cityArray: ["全部","黑龙江", "吉林", "辽宁", "福建", "甘肃", "湖南", "宁夏", "四川", "天津",
            "安徽", "贵州",  "河北", "江西", "内蒙古", "陕西", "西藏", "北京",
            "广西", "河南", "海南", "青海", "上海", "新疆", "重庆", "广东",
            "湖北", "江苏", "蒙授", "山东", "山西", "云南", "浙江"
        ],
        schoolTypeArray: ["全部", "农林类", "政法类", "语言类", "综合类", "医药类",
            "艺术类", "其他类", "理工类", "师范类", "民族类", "财经类", "体育类", "军事类"
        ],
        schoolfeatureArray: ["不限", "985", "211", "双一流", "国重点", "省重点"],
        schoolnatureArray: ["不限", "公办", "民办", "中外合作办学", "港澳台合作办学"],
        searchString:'', // 搜索内容

        lot:'本+专', // 批次
        province:'全部', // 省份
        schoolType:'全部', // 院校类型
        schoolFeature:'不限', // 院校特色
        schoolNature:'不限', // 办学性质
        showLot:'本+专', // 批次(展示)
        showProvince:'全部', // 省份(展示)
        showschoolType:'全部', // 院校类型(展示)
        showschoolFeature:'不限', // 院校特色(展示)
        showschoolNature:'不限', // 办学性质(展示)
        pageNo:1,
        totalPages:0,
        nomore:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this.loadRequest(false)
    },

    // 弹窗选项点击事件
    showSelectAction:function(event) {
        console.log(event)
        if (this.data.screenType == 0) { // 0 学历  1省份  2类型  3其他
            this.setData({
                showLot:event.currentTarget.dataset.item
                
            })
        } else if (this.data.screenType == 1) {
            this.setData({
                showProvince:event.currentTarget.dataset.item
            })
        } else if (this.data.screenType == 2) {
            this.setData({
                showschoolType:event.currentTarget.dataset.item
            })
        } else if (this.data.screenType == 3) {
            this.setData({
                showschoolFeature:event.currentTarget.dataset.item
            })
        }
    },
    showNatureSelectAction:function(event) {
        this.setData({
            showschoolNature:event.currentTarget.dataset.item
        })
    },
    // 确定
    makeAction() {
        let that = this
        if (this.data.screenType == 0) { // 0 学历  1省份  2类型  3其他
            console.log(that.data.showLot)
            this.setData({
                lot:that.data.showLot,
                [`screenArray[${0}]`]: that.data.showLot
            })
            console.log(this.data.lot)
        } else if (this.data.screenType == 1) {
            this.setData({
                province:that.data.showProvince,
                [`screenArray[${1}]`]: that.data.showProvince
            })
        } else if (this.data.screenType == 2) {
            this.setData({
                schoolType:that.data.showschoolType,
                [`screenArray[${2}]`]: that.data.showschoolType
            })
        }  else if (this.data.screenType == 3) {
            let itemContent = ''
            if (that.data.showschoolFeature == '不限' && that.data.showschoolNature == '不限') {
                itemContent = '其他'
            }
            if (that.data.showschoolFeature == '不限' && that.data.showschoolNature != '不限') {
                itemContent = that.data.showschoolNature
            }
            if (that.data.showschoolFeature != '不限' && that.data.showschoolNature == '不限') {
                itemContent = that.data.showschoolFeature
            }
            if (that.data.showschoolFeature != '不限' && that.data.showschoolNature != '不限') {
                itemContent = that.data.showschoolFeature + '+' + that.data.showschoolNature
            }

            this.setData({
                schoolFeature:that.data.showschoolFeature,
                schoolNature:that.data.showschoolNature,
                [`screenArray[${3}]`]: itemContent
            })
        }
        this.onClose();
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this.loadRequest(false)
    },

    // 弹窗关闭
    onClose() {
        this.setData({
            show: false
        })
    },



    // 输入框输入事件
    inputEvent: function(e) {
        // e.detail.value 包含输入框的当前值
        var value = e.detail.value;
        // 可以根据 field 来区分不同的输入框，这里只有一个输入框
        this.setData({
            searchString: value // 更新对应的data中的值
        });
      },
      // 搜索点击事件
      handleSearch: function(e) {
    
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this.loadRequest(false)
      },

    // 筛选
    screenSelectAction: function (event) {
        console.log(event)
        let itemindex = event.currentTarget.dataset.index
        this.setData({
            show: true,
            screenType: itemindex
        })
    },

    

    /** 列表点击事件 */
    cellSelectAction(event) {
        console.log(event)
        let token = wx.getStorageSync('token') || '';
            let openid = wx.getStorageSync('openId') || '';
            if (token == '' || openid == '') {
                wx.navigateTo({
                url: '/pages/login/login',
                })
                return
            }
        wx.navigateTo({
            url: '/pages/index/schooldetail/schooldetail?id=' + event.currentTarget.dataset.id,
        })
    },


    loadRequest(isLoading) {
        console.log(this.data.lot)
        let heBatch = this.data.lot == '本+专' ? '' : this.data.lot;
        let heProv = this.data.province == '全部' ? '' : this.data.province;
        let schoolType = this.data.schoolType  == '全部' ? '' : this.data.schoolType;
        let is985 = '';
        let is211 = '';
        let isDfc = '';
        let isImpProv = '';
        let isImpCountry = '';
        let isGov = this.data.schoolNature == '不限' ? '' : this.data.schoolNature;
        if (this.data.schoolFeature == '985') {
            is985 = 1;
        } else if (this.data.schoolFeature == '211') {
            is211 = 1;
            is985 = 0;
        } else if (this.data.schoolFeature == '双一流') {
            isDfc = 1;
        } else if (this.data.schoolFeature == '国重点') {
            isImpProv = 1;
        } else if (this.data.schoolFeature == '省重点') {
            isImpCountry = 1;
        }
        let url = '?schoolName=' + this.data.searchString + '&heBatch=' + heBatch + "&heProv=" + heProv + '&schoolType=' + schoolType + "&is985=" + is985 + '&is211=' + is211 + "&isDfc=" + isDfc + "&isImpProv=" + isImpProv + "&isImpCountry=" + isImpCountry + "&isGov=" + isGov + "&pageNum=" + this.data.pageNo + '&pageSize=10'
        this._GetSchoolList(url, isLoading)
    },

    /** 查院校 */
//     heBatch	本科 或 专科  就传汉字
// heProv	就传省份汉字
// schoolType	就传汉字
// is985	是否985
// is211	是否211
// isDfc	是否双一流
// isImpProv	是否省重点
// isImpCountry	是否国重点
// isGov	办学性质
    _GetSchoolList(data, isLoading) {
        console.log(data)
        console.log(this.data.totalPages)
        console.log(this.data.listArray.length)
        let that = this
        if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
            this.setData({
              nomore: true
            })
            return;
          }
        GetSchoolList(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                that.setData({
                    listArray: that.data.listArray.concat(array),
                    pageNo: that.data.pageNo + 1,
                    totalPages: res.total,
                  });
     
                  if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                        that.setData({
                            nomore: true
                        })
                  } 
            } else {

                Toast(res.msg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
      onPullDownRefresh() {
        console.log('下拉刷新')
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this.loadRequest(false)
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
      },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('上拉加载更多')
        this.loadRequest(false)
    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})